namespace("mk.objects.chars");

mk.objects.chars.Char = function(x, y, id, obj) {
    obj = obj || {};
    var context = this;
    this.id = id;
    this.pos = {
            p1:90
            , l1:-90
            , k1:-90
            , p2:80
            , l2:-50
            , k2:-0
            , korpus:0
            , golova:0
            , bedro1:0
            , koleno1:0
            , stopa1:0
            , bedro2:0
            , koleno2:0
            , stopa2:0
            , y_body_delta:0
    };
    this.angles = {};
    mk.objects.Object.call(this, x, y, id);
    this.__frameIndex = 0;
    this.hits = obj.hits || 100;
    this.speed = obj.speed || 3;
    this.__action = "stay";
    this.moveKX = 0;
    this.moveKY = 0;
    this.direction = obj.direction || 1;
    this.animations = mk.animations.char;
    this.isCollizion = false;
    this.handObjectLeft = mk.weapons.sword1;
    this.handObjectRight = mk.weapons.sword1;

    this.view = new sg.canvas.AnimatedCanvas(mk.constants.CANVAS_WIDTH
        , mk.constants.CANVAS_HEIGHT, this.x, this.y, this).append(document.body);
    this.view.drawFrame = function() {
        context.getAngles();
        this.save()
            .scale(context.direction,1)
            .translate(context.x*context.direction-(context.direction<0 ? 40:0), context.y+context.pos.y_body_delta)
            .save()
            .translate(35, 15)
            .rotate((context.pos.p1).degree())
            .drawObject(mk.bodyBlocks.plech2)
            .translate(27, 0)
            .rotate(context.pos.l1.degree())
            .drawObject(mk.bodyBlocks.predpl2)
            .translate(30, 2)
            .rotate(context.pos.k1.degree())
            .drawObject(context.getLeftObject())
            .drawObject(mk.bodyBlocks.kist2)
            .restore()
            .save()
            .translate(25, 70)
            .rotate(context.pos.bedro2.degree())
            .drawObject(mk.bodyBlocks.bedro2)
            .translate(0, 50)
            .rotate(context.pos.koleno2.degree())
            .drawObject(mk.bodyBlocks.golen2)
            .translate(-2, 36)
            .rotate(context.pos.stopa2.degree())
            .drawObject(mk.bodyBlocks.stopa2)
            .restore()
            .save()
            .translate(12, 70)
            .rotate(context.pos.bedro1.degree())
            .drawObject(mk.bodyBlocks.bedro1)
            .translate(0, 50)
            .rotate(context.pos.koleno1.degree())
            .drawObject(mk.bodyBlocks.golen1)
            .translate(-2, 36)
            .rotate(context.pos.stopa1.degree())
            .drawObject(mk.bodyBlocks.stopa1)
            .restore()
            .save()
            .drawObject(mk.bodyBlocks.body)
            .translate(15, -28)
            .drawObject(mk.bodyBlocks.head)
            .restore()
            .save()
            .translate(7, 13)
            .rotate(context.pos.p2.degree())
            .drawObject(mk.bodyBlocks.plech2)
            .translate(27, 0)
            .rotate(context.pos.l2.degree())
            .drawObject(mk.bodyBlocks.predpl2)
            .translate(30, 2)
            .rotate(context.pos.k2.degree())
            .drawObject(context.getRightObject())
            .drawObject(mk.bodyBlocks.kist2)
            .restore()
            .restore()
            .draw();
    };
    this.view.addEventListener("before_frame_draw", function() {
        context.setFrame();
        context.x += context.moveKX;
        context.y += context.moveKY;
    });
    this.view.addEventListener("after_frame_draw", function() {
        context.getCollisionPoints();
    });

};

mk.objects.chars.Char.extends(mk.objects.Object);

mk.objects.chars.Char.prototype.setFrame = function(index) {

    var _frame = this.animations[this.__action][this.__frameIndex];
    for(var i in _frame) {
        this.pos[i] = _frame[i];
    }
    this.__frameIndex++;
    if(this.__frameIndex >= this.animations[this.__action].length) {
        this.__frameIndex = 0;
    }
};

mk.objects.chars.Char.prototype.action = function(name) {
    this.__frameIndex = 0;
    this.__action = name;
};
mk.objects.chars.Char.prototype.__getCollisionPoint = function(hand, weapon) {
    var alpha, beta, delta, dx, dy;
    switch(hand) {
        case "left":
            alpha = this.pos.l1-(90-this.pos.p1);
            beta = this.pos.k1-180 + alpha;
            delta = this.pos.p1;
            dx = 35;
            dy = 15;
            break;
        case "right":
            alpha = this.pos.l2-(90-this.pos.p2);
            beta = this.pos.k2-180 + alpha;
            delta = this.pos.p2;
            dx = 5;
            dy = 10;
            break;
    }
    var x1= this.x + (dx
        + Math.cos((delta).degree())*30
        - Math.sin((alpha).degree())*37-(this.direction<0 ? 40:0)
        + Math.sin((beta).degree())*(weapon.width-20)-(this.direction<0 ? 40:0))*this.direction;
    var y1 = this.y + dy
        + Math.sin((delta).degree())*30
        + Math.cos((alpha).degree())*37
        - Math.cos((beta).degree())*(weapon.width-20);
    return {x:x1, y:y1};
};

mk.objects.chars.Char.prototype.getCollisionPoints = function() {
    var arr = [];
    var pointsLeft =  this.__getCollisionPoint("left", this.getLeftObject());
    pointsLeft.key = this.getLeftObject().key;
    pointsLeft.sender = this;
    var pointsRight = this.__getCollisionPoint("right", this.getRightObject());
    pointsRight.key = this.getRightObject().key;
    pointsRight.sender = this;
    arr.push(pointsLeft);
    arr.push(pointsRight);
    return arr;
};
mk.objects.chars.Char.prototype.checkCollisions = function(arr) {
     this.view.checkCollisions(arr);
};

mk.objects.chars.Char.prototype.getLeftObject = function() {
      return this.handObjectLeft;
};

mk.objects.chars.Char.prototype.getRightObject = function() {
    return this.handObjectRight;
};

mk.objects.chars.Char.prototype.setAngles = function(angles) {
    if(angles)  {
        this.angles = angles;
    }
};

mk.objects.chars.Char.prototype.getAngles = function()
{
    var k1 = -3;
    var k2 = 7;
    var k3 = 3;


    var actionFlag = false;
    var notActionFlag = false;
    var clearFlag = false;
    var _opponents=[],  _points;
    for(var i in this.angles) {
        if(this.pos[i]>this.angles[i]+k1 && this.pos[i]<this.angles[i]-k1) {
            this.pos[i] = this.angles[i];
            document.getElementById("log").innerHTML="move";
        }
        else if(this.pos[i] > this.angles[i] + k1) {
            this.pos[i] += k1;
            this.isCollizion = false;
            document.getElementById("log").innerHTML="up";
        }
        else if(this.pos[i] < this.angles[i]+k3 && !this.isCollizion) {
            this.pos[i] += k2;
            actionFlag = true;
            document.getElementById("log").innerHTML="kick";
        }
    }

    if(actionFlag) {
        _opponents = mk.Application.getOpponents(this.id);
        _points = this.getCollisionPoints();
        for(var i =0; i< _opponents.length; i++ ) {
            _opponents[i].checkCollisions(_points);
        }
    }
    else  {
        _opponents = mk.Application.getOpponents(this.id);
        for(var i =0; i< _opponents.length; i++ ) {
            _opponents[i].checkCollisions([]);
        }
    }
};
mk.objects.chars.Char.prototype.onCollision = function() {
    for(var i in this.angles) {
        this.angles[i] = this.pos[i];
    }
    this.isCollizion = true;
};
mk.objects.chars.Char.prototype.onNotCollision = function() {
    this.isCollizion = false;
};

mk.objects.chars.Char.prototype.destroy = function() {
    this.view.destroy();
};







